//
//  Closures.swift
//  BJ12366Swift
//
//  Created by ZRAR on 16/2/22.
//  Copyright © 2016年 ZRAR. All rights reserved.
//

import UIKit

class Closures: NSObject {
    
    //非逃逸闭包，函数执行完闭包被销毁
    func someFunctionWithNoescapeClosure(@noescape closure:()->Void){
        closure()
    }
    var completionHandlers:[()->Void] = []
    //逃逸闭包
    func someFunctionWithEscapingClosure(completionHandler:()->Void){
        
        completionHandlers += [completionHandler]

    }
    var x = 10
    func doSomething(){
        someFunctionWithEscapingClosure { () -> Void in
            
            self.x = 100
        }
        someFunctionWithNoescapeClosure { () -> Void in
            x = 200
        }
        var customerslnLine = ["Chris","Alex","Ewa","Barry","Daniella"]
        
        let customerProvider = {customerslnLine.removeFirst()}
        print(customerslnLine.count)
        print("Now Serving\(customerProvider())")
        print(customerslnLine.count)
        print("Now Sering\(customerProvider())")
        print(customerslnLine.count)
        
    }
    //将闭包作为参数传递给函数时，获得同样的延时求值行为
    func serveCustome(customerProvider:()->String){
        
        print("Now serving\(customerProvider())")
    }
    //自动闭包,@autoclosure暗含了@noescape特性，想要让他逃逸@autoclosure(escaping)
    func serverautCustomers(@autoclosure customerProvider:()->String){
        print("Now serving \(customerProvider())")
        
    }
    
    
}
